$(function () {
    $.get(baseURL + "promotion/configcouponucenter/getCouponList", function (r) {
        vm.couponList = r.couponList
        vm.payList = r.payList
        vm.userTypeList = r.sendUserTypeEnum
        vm.sendCouponTypeEnum = r.sendCouponTypeEnum
    });
});

var vm = new Vue({
    el: '#rrapp',
    data() {
        return {
            showList: true,
            couponList: [],//优惠券列表
            payList: [],//支付券列表
            sendType: '',
            couponIds: '',//优惠券ids
            userIds: '',
            quantity: 1,
            userTypeList: [],
            sendCouponTypeEnum:[],
            userType: '',
            userSource:[],
            userlevel:[],
            sendCouponType:'userIds',
            typeOptions: [{
                value: 'coupon',
                label: '优惠券'
            }, {
                value: 'pay',
                label: '支付券'
            }],
        }
    },
    methods: {
        sumbit() {
            let couponType = ''
            if (vm.sendType === 'coupon') {
                couponType = 'coupon' //优惠券
            }
            if (vm.sendType === 'pay') {
                couponType = 'pay' //支付券
            }
            if(!this.sendCouponType||!this.sendType||!this.couponIds||!this.quantity||this.quantity<1){
                alert('请输入正确内容!')
                return
            }
            let param;
            let count;
            if (vm.sendCouponType=='userType') {
                param=vm.userType
                $.get(baseURL + "promotion/sendCoupon/getCount/"+param+"/"+vm.sendCouponType, function (r) {
                    count = r.count
                    confirm('确定对'+count+'个用户发送？', function () {
                        $.ajax({
                            type: "POST",
                            url: baseURL + "promotion/sendCoupon/doBatchSendCoupon",
                            dataType: "json",
                            data: {
                                'sendCouponType':vm.sendCouponType,
                                'couponIds': vm.couponIds.join(','),
                                'param': param,
                                'quantity': vm.quantity,
                                'remarks': '批量发券'
                            },
                            success: function (r) {
                                if (r.code === 0) {
                                    alert('操作成功');
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                })
            }else if (vm.sendCouponType == 'userSource') {
                param=vm.userSource.join(',')
                $.get(baseURL + "promotion/sendCoupon/getCount/"+param+"/"+vm.sendCouponType, function (r) {
                    count = r.count
                    confirm('确定对'+count+'个用户发送？', function () {
                        $.ajax({
                            type: "POST",
                            url: baseURL + "promotion/sendCoupon/doBatchSendCoupon",
                            dataType: "json",
                            data: {
                                'sendCouponType':vm.sendCouponType,
                                'couponIds': vm.couponIds.join(','),
                                'param': param,
                                'quantity': vm.quantity,
                                'remarks': '批量发券'
                            },
                            success: function (r) {
                                if (r.code === 0) {
                                    alert('操作成功');
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                })
            }else if (vm.sendCouponType == 'userLevel') {
                param=vm.userlevel.join(',')
                $.get(baseURL + "promotion/sendCoupon/getCount/"+param+"/"+vm.sendCouponType, function (r) {
                    count = r.count
                    confirm('确定对'+count+'个用户发送？', function () {
                        $.ajax({
                            type: "POST",
                            url: baseURL + "promotion/sendCoupon/doBatchSendCoupon",
                            dataType: "json",
                            data: {
                                'sendCouponType':vm.sendCouponType,
                                'couponIds': vm.couponIds.join(','),
                                'param': param,
                                'quantity': vm.quantity,
                                'remarks': '批量发券'
                            },
                            success: function (r) {
                                if (r.code === 0) {
                                    alert('操作成功');
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                })
            }else{
                param=vm.userIds
               const str=vm.userIds.replace(new RegExp("，","gm"),",").replace(new RegExp("，","gm"),",").replace(new RegExp(" ","gm"),"")
                if (!(/^[\d\,]*$/).test(str)){
                    alert("请输入会员数字ID,以逗号分隔!")
                    return
                }
               count= str.split(',').length
                confirm('确定对'+count+'个用户发送？', function () {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "promotion/sendCoupon/doBatchSendCoupon",
                        dataType: "json",
                        data: {
                            'sendCouponType':vm.sendCouponType,
                            'couponIds': vm.couponIds.join(','),
                            'param': param,
                            'quantity': vm.quantity,
                            'remarks': '批量发券'
                        },
                        success: function (r) {
                            if (r.code === 0) {
                                alert('操作成功');
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            }

        },
        clear() {
            vm.couponIds = ''
            vm.userIds = ''
            vm.quantity = ''
        },
    }
});